{
  "properties" : { },
  "id" : "a8c942fe51d14b2fa71d9bb307ac410f",
  "script" : null,
  "groupId" : "ae84b87a26724813a446a132163ff3ca",
  "name" : "登录注册",
  "createTime" : null,
  "updateTime" : 1741367987229,
  "lock" : null,
  "createBy" : null,
  "updateBy" : null,
  "path" : "/login",
  "method" : "POST",
  "parameters" : [ ],
  "options" : [ {
    "name" : "require_login",
    "value" : "false",
    "description" : "该接口需要登录才允许访问",
    "required" : false,
    "dataType" : "String",
    "type" : null,
    "defaultValue" : null,
    "validateType" : null,
    "error" : null,
    "expression" : null,
    "children" : null
  } ],
  "requestBody" : "{\n    \"username\":\"admin123456\",\n    \"password\":\"123456\",\n    \"type\":\"login\",\n    \"deviceId\":\"123432\"\n    \n}",
  "headers" : [ ],
  "paths" : [ ],
  "responseBody" : "{\n    \"code\": 200,\n    \"message\": \"success\",\n    \"data\": {\n        \"id\": 6,\n        \"username\": \"admin123456\",\n        \"token\": \"993b004d-4d42-4e2e-9001-fbf8c018e7ac\"\n    },\n    \"timestamp\": 1741283204981,\n    \"executeTime\": 675\n}",
  "description" : null,
  "requestBodyDefinition" : {
    "name" : "root",
    "value" : "",
    "description" : "",
    "required" : false,
    "dataType" : "Object",
    "type" : null,
    "defaultValue" : null,
    "validateType" : "",
    "error" : "",
    "expression" : "",
    "children" : [ {
      "name" : "username",
      "value" : "test12",
      "description" : "",
      "required" : false,
      "dataType" : "String",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "password",
      "value" : "123123",
      "description" : "",
      "required" : false,
      "dataType" : "String",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "type",
      "value" : "reg",
      "description" : "",
      "required" : false,
      "dataType" : "String",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "deviceId",
      "value" : "111",
      "description" : "",
      "required" : false,
      "dataType" : "String",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    } ]
  },
  "responseBodyDefinition" : {
    "name" : "",
    "value" : "",
    "description" : "",
    "required" : false,
    "dataType" : "Object",
    "type" : null,
    "defaultValue" : null,
    "validateType" : "",
    "error" : "",
    "expression" : "",
    "children" : [ {
      "name" : "code",
      "value" : "200",
      "description" : "",
      "required" : false,
      "dataType" : "Integer",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "message",
      "value" : "success",
      "description" : "",
      "required" : false,
      "dataType" : "String",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "data",
      "value" : "",
      "description" : "",
      "required" : false,
      "dataType" : "Object",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ {
        "name" : "id",
        "value" : "6",
        "description" : "",
        "required" : false,
        "dataType" : "Integer",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "username",
        "value" : "admin123456",
        "description" : "",
        "required" : false,
        "dataType" : "String",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      }, {
        "name" : "token",
        "value" : "993b004d-4d42-4e2e-9001-fbf8c018e7ac",
        "description" : "",
        "required" : false,
        "dataType" : "String",
        "type" : null,
        "defaultValue" : null,
        "validateType" : "",
        "error" : "",
        "expression" : "",
        "children" : [ ]
      } ]
    }, {
      "name" : "timestamp",
      "value" : "1741283204981",
      "description" : "",
      "required" : false,
      "dataType" : "Long",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    }, {
      "name" : "executeTime",
      "value" : "675",
      "description" : "",
      "required" : false,
      "dataType" : "Integer",
      "type" : null,
      "defaultValue" : null,
      "validateType" : "",
      "error" : "",
      "expression" : "",
      "children" : [ ]
    } ]
  }
}
================================
import cn.hutool.core.util.StrUtil
import cn.hutool.core.util.RandomUtil
import cn.hutool.core.util.ReUtil;
import log
import 'cn.dev33.satoken.secure.SaSecureUtil';
import 'cn.dev33.satoken.stp.StpUtil';
import 'cn.hutool.core.lang.Validator';
import 'cn.hutool.core.util.StrUtil';
import cn.hutool.core.date.DateUtil;
import env;
import request;
import http;

var username = body.username
var password = body.password
var type = body.type
let deviceId = body.deviceId

var user

if (type == 'reg') {
    userInfo = db.table("users").where().eq("username", username).selectOne()
    if (userInfo != null) {
        exit 400, '用户已存在';
    }
    encrypt = RandomUtil.randomString(6)
    pwd = SaSecureUtil.md5(SaSecureUtil.md5(password) + encrypt)
    db_res = db.table("users").insert({
        encrypt: encrypt,
        username: username,
        password: pwd,
        createDate: now(),
        deviceFingerprint: deviceId
    })

    //默认订阅套餐1，也就是免费套餐
    db.table("subscription").insert({
        userId: db_res,
        packageId: 1,
        startTime: now(),
        endTime: DateUtil.parse("2099-12-31 23:59:59").toLocalDateTime(),
        createBy: db_res,
        createDate: now(),
    })      
    //余额表也插入
    db.table("credit").insert({
        userId: db_res,
        packageId: 1,
        remainCount: 6,
        createBy: db_res,
        createDate: now(),
        expireDate:DateUtil.parse("2099-12-31 23:59:59").toLocalDateTime(),
    })
    //执行登录,返回token
    StpUtil.login(db_res)
    var token = StpUtil.getTokenValueByLoginId(db_res)

    db.table("users").primary("id").update({
        id: db_res,
        current_token: token
    })

    return { //登录成功直接登录
        id: db_res,
        username,
        token
    }
} else {
    userInfo = db.table("users").where().eq("username", username).selectOne()
    if (userInfo == null) {
        exit 400, '用户不存在';
    }

    log.info("userInfo-->" + userInfo)
    log.info("userInfo id-->" + userInfo.id)
    pwd = SaSecureUtil.md5(SaSecureUtil.md5(password) + userInfo.encrypt)
    if (userInfo.password != pwd) {
        exit 0, '用户名或密码错误'
    }

    if (body.deviceId != userInfo.deviceFingerprint) {
        //新设备登录，这里要踢掉旧的记录
        StpUtil.logout(userInfo.id)
    }

    //更新用户id和最后登录时间

    var data = {
        id,
        login_num,
        last_ip,
        last_at,
        line_ip,
        line_at,
    }

    ip = request.getClientIP()
    data.id = userInfo.id
    data.login_num = (userInfo.loginNum ? userInfo.loginNum : 0) + 1
    data.last_ip = ip
    data.last_at = now()
    data.line_ip = ip
    data.line_at = now()
    data.deviceFingerprint = body.deviceId
    db.table('users').primary('id').update(data, true)
    //执行登录 这里都要重新登录
    StpUtil.login(userInfo.id)
    var token = StpUtil.getTokenValueByLoginId(userInfo.id)
    userInfo.token = token
    userInfo.password = ""
    return {
        id: userInfo.id,
        username,
        token
    }
}